package com.rest.demo.io;

import jdk.internal.util.xml.impl.Input;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-05 9:14
 */
public class ByteArrayDemo {

    public static void main(String[] args) {

        //java8的方式
        String str = null;
        try {
            str = Files.readAllLines(Paths.get("F:\\a.txt")).stream()
                    .collect(Collectors.joining("\n"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(str);

        //传统方式
        InputStream is = null;
        OutputStream os = null;
        StringBuffer str2 = new StringBuffer();
        try {
            os = new FileOutputStream("F:\\a-copy.txt");
            is = new FileInputStream(new File("F:\\a.txt"));
            byte[] buff = new byte[1024];
            int b = -1;
            while((b = is.read(buff)) != -1) {
                str2.append(new String(buff,0,b));
                os.write(buff,0,b);
                os.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(str2.toString());
    }
}
